// 禁止多次点击
function noMultipleClicks(methods, info) {
	let that = this;
	if (that.noClick) {
		//第一次点击
		that.noClick = false;
		if ((info && info != '') || info == 0) {
			//info是执行函数需要传的参数
			methods(info);
		} else {
			methods();
		}
		setTimeout(() => {
			that.noClick = true;
		}, 2000)
	} else {

	}
}

function formtNumber(e) {
	if (e == 'undefined' || e == undefined || e == null) {
		return '--'
	} else {
		return e
	}
}

// 获取当前时间
function getCurrentFormattedDateTime(days) {
    const currentDate = new Date();
    if (days) {
        currentDate.setDate(currentDate.getDate() - days);
    }
    const year = currentDate.getFullYear();
    const month = String(currentDate.getMonth() + 1).padStart(2, '0');
    const day = String(currentDate.getDate()).padStart(2, '0');
    const hours = String(currentDate.getHours()).padStart(2, '0');
    const minutes = String(currentDate.getMinutes()).padStart(2, '0');
    const seconds = String(currentDate.getSeconds()).padStart(2, '0');

    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

// 日期格式化
function parseTime(time, pattern) {
    if (arguments.length === 0 || !time) {
        return null
    }
    const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
    let date
    if (typeof time === 'object') {
        date = time
    } else {
        if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
            time = parseInt(time)
        }
        if ((typeof time === 'number') && (time.toString().length === 10)) {
            time = time * 1000
        }
        date = new Date(time)
    }
    const formatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds(),
        a: date.getDay()
    }
    const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
        let value = formatObj[key]
        // Note: getDay() returns 0 on Sunday
        if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
        if (result.length > 0 && value < 10) {
            value = '0' + value
        }
        return value || 0
    })
    return time_str
}

/** 
* formatCloudSpeed 根据风速返回对应的风力
* @关键字 val 风速
*/
function formatCloudSpeed(val) {
    if (val < 1.6) {
        return '1'
    } else if (val >= 1.6 && val <= 3.3) {
        return '2'
    } else if (val >= 3.4 && val <= 5.4) {
        return '3'
    } else if (val >= 5.5 && val <= 7.9) {
        return '4'
    } else if (val >= 8.0 && val <= 10.7) {
        return '5'
    } else if (val >= 10.8 && val <= 13.8) {
        return '6'
    } else if (val >= 13.9 && val <= 17.1) {
        return '7'
    } else if (val >= 17.2 && val <= 20.7) {
        return '8'
    } else if (val >= 20.8 && val <= 24.4) {
        return '9'
    } else if (val >= 24.5 && val <= 28.4) {
        return '10'
    } else if (val >= 28.5 && val <= 32.6) {
        return '11'
    } else if (val >= 32.7 && val <= 36.9) {
        return '12'
    } else {
        return '13'
    }
}

// 数组按照某个key进行排序
function sortBykey(ary, key) {
    return ary.sort(function (a, b) {
        let x = a[key] || 0
        let y = b[key] || 0
        return ((x < y) ? 1 : (x > y) ? -1 : 0)
    })
}

/** 
* isEmpty  判断字段是否为空 ，返回固定值
* @关键字 val 用于判断的值
* @关键字 text 如果为空，返回该值代替,不传默认返回‘-’
*/
function isEmpty(val, text) {
    if (val === null || val === undefined || val === '' || val==='null' || val=== 'undefined' || isNaN(val)) {
        return (text || '-')
    } else {
        return val
    }
}

// 获取风向
function getWindDirection(dir, speed) {
    var retString = "";
    if (speed == 0) {
        retString = "静风";
    }
    else {
        var grade = 0;
        var val = parseFloat(dir) + 11.25;
        if (val < 360) {
            grade = parseInt(Math.floor(val / 22.5));
        }
        else {
            grade = 0;
        }
        switch (grade) {
            case 0:
                retString = "北";
                break;
            case 1:
                retString = "东北偏北";
                break;
            case 2:
                retString = "东北";
                break;
            case 3:
                retString = "东北偏东";
                break;
            case 4:
                retString = "东";
                break;
            case 5:
                retString = "东南偏东";
                break;
            case 6:
                retString = "东南";
                break;
            case 7:
                retString = "东南偏南";
                break;
            case 8:
                retString = "南";
                break;
            case 9:
                retString = "西南偏南";
                break;
            case 10:
                retString = "西南";
                break;
            case 11:
                retString = "西南偏西";
                break;
            case 12:
                retString = "西";
                break;
            case 13:
                retString = "西北偏西";
                break;
            case 14:
                retString = "西北";
                break;
            case 15:
                retString = "西北偏北";
                break;
        }
    }
    return retString;
}

// 根据经纬度获取两点距离
function getDistance(lat1, lng1, lat2, lng2) {
    let radLat1 = lat1 * Math.PI / 180.0;
    let radLat2 = lat2 * Math.PI / 180.0;
    let a = radLat1 - radLat2;
    let b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
    let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math
        .pow(Math.sin(b / 2), 2)));
    s = s * 6378.137;
    s = Math.round(s * 10000) / 10000;
    let distance = s.toString();
    let distance_str = "";
    distance = distance.substring(0, distance.indexOf('.') + 2);
    distance_str = Math.round(distance * 1000);
    return distance_str
    // if (parseInt(distance) >= 1) {
    //     distance_str = distance + "km";
    //     return distance_str
    // } else {
    //     distance_str = (distance * 1000) + "m";
    //     return distance_str
    // }
}



function formatWeather(val, type) {
    if (val == '0.0') {  //晴
            if (type == 1) {
                return '0'
            } else {
                return '晴'
            }
        } else if (val == '1.0') {  //多云
            if (type == 1) {
                return '01'
            } else {
                return '多云'
            }
        } else if (val == '2.0') {  //阴
            if (type == 1) {
                return '02'
            } else {
                return '阴'
            }
        } else if (val == '3.0') {  //阵雨
            if (type == 1) {
                return '03'
            } else {
                return '阵雨'
            }
        } else if (val == '4.0') {  //雷阵雨
            if (type == 1) {
                return '04'
            } else {
                return '雷阵雨'
            }
        } else if (val == '5.0') {  //雷阵雨并伴有冰雹
            if (type == 1) {
                return '05'
            } else {
                return '雷阵雨并伴有冰雹'
            }
        } else if (val == '6.0') {  //雨夹雪
            if (type == 1) {
                return '06'
            } else {
                return '雨夹雪'
            }
        } else if (val == '7.0') {  //小雨
            if (type == 1) {
                return '07'
            } else {
                return '小雨'
            }
        } else if (val == '8.0') {  //中雨
            if (type == 1) {
                return '08'
            } else {
                return '中雨'
            }
        } else if (val == '9.0') {  //大雨
            if (type == 1) {
                return '09'
            } else {
                return '大雨'
            }
        } else if (val == '10.0') {  //暴雨
            if (type == 1) {
                return '10'
            } else {
                return '暴雨'
            }
        } else if (val == '11.0') {  //大暴雨
            if (type == 1) {
                return '11'
            } else {
                return '大暴雨'
            }
        } else if (val == '12.0') {  //特大暴雨
            if (type == 1) {
                return '12'
            } else {
                return '特大暴雨'
            }
        } else if (val == '13.0') {  //阵雪
            if (type == 1) {
                return '13'
            } else {
                return '阵雪'
            }
        } else if (val == '14.0') {  //小雪
            if (type == 1) {
                return '14'
            } else {
                return '小雪'
            }
        } else if (val == '15.0') {  //中雪
            if (type == 1) {
                return '15'
            } else {
                return '中雪'
            }
        } else if (val == '16.0') {  //大雪
            if (type == 1) {
                return '16'
            } else {
                return '大雪'
            }
        } else if (val == '17.0') {  //暴雪
            if (type == 1) {
                return '17'
            } else {
                return '暴雪'
            }
        } else if (val == '18.0') {  //雾
            if (type == 1) {
                return '18'
            } else {
                return '雾'
            }
        } else if (val == '19.0') {  //冻雨
            if (type == 1) {
                return '19'
            } else {
                return '冻雨'
            }
        } else if (val == '20.0') {  //沙尘暴
            if (type == 1) {
                return '20'
            } else {
                return '沙尘暴'
            }
        } else if (val == '29.0') {  //浮尘
            if (type == 1) {
                return '29'
            } else {
                return '浮尘'
            }
        } else if (val == '30.0') {  //扬沙
            if (type == 1) {
                return '30'
            } else {
                return '扬沙'
            }
        } else if (val == '31.0') {  //强沙尘暴
            if (type == 1) {
                return '20'
            } else {
                return '强沙尘暴'
            }
        } else if (val == '32.0') {  //雨
            if (type == 1) {
                return '32'
            } else {
                return '雨'
            }
        } else if (val == '33.0') {  //雪
            if (type == 1) {
                return '33'
            } else {
                return '雪'
            }
        } else if (val == '53.0') {  //霾
            if (type == 1) {
                return '53'
            } else {
                return '霾'
            }
        }
}

export default {
	noMultipleClicks,
	formtNumber,
	getDistance,
	getWindDirection,
	getCurrentFormattedDateTime,	parseTime,	formatCloudSpeed,	sortBykey,	isEmpty,
	formatWeather}